package it.polimi.swKnights.SWIMv2.servlets.help;

import it.polimi.swKnights.SWIMv2.ejb.exceptions.InvalidLoginPrivilegesException;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.LoginFailException;
import it.polimi.swKnights.SWIMv2.ejb.helpRequest.SeeHelpRequestRemote;
import it.polimi.swKnights.SWIMv2.general.Errors;
import it.polimi.swKnights.SWIMv2.general.WebsiteParameters;
import it.polimi.swKnights.SWIMv2.servlets.SwimServlet;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.ejb.EJBException;
import javax.persistence.EntityNotFoundException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class SeeHelpRequestController
 */
public class SeeHelpRequestController extends SwimServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{

		String destination = WebsiteParameters.seeHelpRequestsFormUrl;

		HttpSession session = request.getSession();
		String userName = (String) session.getAttribute("userName");
		String password = (String) session.getAttribute("password");

		SeeHelpRequestRemote reqRem = this.lookUp(SeeHelpRequestRemote.class);

		List<Map<String, String>> mapListHelpRequested = null;

		String parameter = "";
		try
		{
			mapListHelpRequested = reqRem.getHelpRequests(userName, password);

			request.setAttribute("help_requests", mapListHelpRequested);
			request.getRequestDispatcher(destination)
					.forward(request, response);
		}
		catch (EJBException e)
		{

			parameter = "?error=" + Errors.noexistuser;
			response.sendRedirect("./siteHome" + parameter);
		}
		catch (InvalidLoginPrivilegesException e)
		{
			parameter = "?error=" + Errors.notuser;
			response.sendRedirect("./siteHome" + parameter);
		}
		catch (EntityNotFoundException e)
		{
			parameter = "?error=" + Errors.notfound;
			response.sendRedirect("./siteHome" + parameter);
		}
		catch (LoginFailException e)
		{
			parameter = "?error=" + Errors.session;
			response.sendRedirect("./siteHome" + parameter);
		}

	}

}
